//
//  AdvertViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AdvertViewController: BaseViewController {
    var viewModel:AdvertViewViewModel?
    lazy var advertView:AdvertView = {
        let view = AdvertView(dataSource: viewModel)
        return view
    }()
    
    lazy var rightBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle(kText("发布"), for: UIControlState.normal)
        btn.setTitleColor(kThemeColor, for: UIControlState.normal)
        btn.addTarget(self, action: #selector(rightBtnAction), for: UIControlEvents.touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        let size = btn.titleLabel?.sizeWithText(text: kText("发布") as NSString, font: (btn.titleLabel?.font)!, size: CGSize(width: 100, height: 44))
        btn.bounds = CGRect(x: 0, y: 0, width: (size?.width)!, height: 44)
        return btn
    }()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        viewModel = AdvertViewViewModel(controller:self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.title = (viewModel?.isBuyBool)! ? kText("发布购买广告") : kText("发布出售广告")
        viewModel?.archiveCellKind()
        view.addSubview(advertView)
        
        //如果是未修改的 初始化默认BTC
        if !(viewModel?.isChange)! {
            viewModel?.archiveCoinInformation(coinName: "BTC", block:{
                DispatchQueue.main.async {
                    self.viewModel?.refreshBool = true
                }
            })
        }else {
            viewModel?.archiveAdverInformation()
            rightBtn.setTitle(kText("修改"), for: UIControlState.normal)
        }
        advertView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        //如果是出售获取法币资产
        if !(viewModel?.isBuyBool)! {
        viewModel?.archiveLegalAmount()
        }
        
        self.creatRightItem()
    }
    
    func creatRightItem() -> Void {
        let item  = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = item
    }
    @objc func rightBtnAction() -> Void {
        self.viewModel?.sendInformation()
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
